home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / BOUNCE_C / BOUNCEAB.C < prev    next >
Text File  |  1990-11-21  |  3KB  |  143 lines

  1. /*
  2.  * bounceabout.c -- this file contains the routines to put up and housekeep the About
  3.  * Box.  This code is harder to write than the cdev code it comments on!  To put a
  4.  * LineBox object in this dialog required a filter proc.  Since objects use the A4
  5.  * based jump table (in code resources), we must use Remember/SetUp/RestoreA4 if we
  6.  * want to make an object call from our filter proc.  Plus, since we're using Modal-
  7.  * Dialog, not Alert, we have to draw the 'default frame' for our OK button.
  8.  */
  9. #include "bounce.h"
  10. #include <SetUpA4.h>
  11.  
  12. /* prototypes for functions in this file */
  13.  
  14. pascal void DrawProc(DialogPtr, short);
  15. pascal Boolean filter(DialogPtr, EventRecord *, short *);
  16. void DoIdle(DialogPtr);
  17. void DoCr(DialogPtr);
  18. void DoUpdate(DialogPtr);
  19. void FrameDefault(DialogPtr, int item);
  20.  
  21. /*
  22.  * This function puts up the dialog box and repeatedly calls ModalDialog.
  23.  */
  24. void BounceAbout(DialogPtr aboutBox, int type, Rect *box)
  25. {
  26.     int itemHit;
  27.  
  28.     RememberA4();
  29.     SetDItem(aboutBox, AboutBounceItem, type, (Handle)DrawProc, box);
  30.     ShowWindow((WindowPtr) aboutBox);
  31.     do {
  32.         ModalDialog(filter, &itemHit);
  33.     } while (itemHit != OKItem);
  34. }
  35.  
  36. /*
  37.  * This function calls the LineBox's Draw routine.
  38.  */
  39. static pascal void DrawProc(DialogPtr dp, short item)
  40. {
  41.     SetUpA4();
  42.     ((LineBox *)GetWRefCon((WindowPtr) dp))->Draw();
  43.     RestoreA4();
  44. }
  45.  
  46. /*
  47.  * This is the ModalDialog filter proc.  It responds to update and keyDown events,
  48.  * and during nullEvents it calls the LineBox's idle procedure.
  49.  */
  50. static pascal
  51. Boolean filter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  52. {
  53.     long keyhit;
  54.     Boolean result = FALSE;
  55.  
  56.     switch(theEvent->what) {
  57.     case updateEvt:
  58.         if ((DialogPtr)theEvent->message == theDialog) {
  59.             DoUpdate(theDialog);
  60.             *itemHit = 0;
  61.             result = TRUE;
  62.         }
  63.         break;
  64.     case keyDown:
  65.         keyhit = theEvent->message & charCodeMask;
  66.         if (keyhit == 0x3 || keyhit == 0xd) {
  67.             DoCr(theDialog);
  68.             *itemHit = OKItem;
  69.             result = TRUE;
  70.         }
  71.         break;
  72.     case nullEvent:
  73.         DoIdle(theDialog);
  74.         break;
  75.     default:
  76.         break;
  77.     }
  78.     return result;
  79. }
  80.  
  81. /*
  82.  * During idle time, call the LineBox's Idle procedure to bounce the line.
  83.  */
  84. static void DoIdle(DialogPtr theDialog)
  85. {
  86.     SetUpA4();
  87.     ((LineBox *)GetWRefCon((WindowPtr) theDialog))->Idle();
  88.     RestoreA4();
  89. }
  90.  
  91. /*
  92.  * This function handles the user hitting return or enter.  It selects the
  93.  * OK button with HiliteControl, then waits for a bit so the user can see it.
  94.  */
  95. static void DoCr(DialogPtr theDialog)
  96. {
  97.     ControlHandle okButton;
  98.     int type;
  99.     Rect box;
  100.     long dummy;
  101.  
  102.     GetDItem(theDialog, OKItem, &type, (Handle *)&okButton, &box);
  103.     HiliteControl(okButton, 1);
  104.     Delay(3, &dummy);
  105. }
  106.  
  107. /*
  108.  * Since we want to draw a button outline, we have to take care of all the
  109.  * update drawing for our dialog.  (Of course, one could use a UserItem with
  110.  * a Draw proc...)
  111.  */
  112. static void DoUpdate(DialogPtr theDialog)
  113. {
  114.     GrafPtr savePort;
  115.  
  116.     GetPort(&savePort);
  117.     SetPort(theDialog);
  118.     BeginUpdate(theDialog);
  119.     UpdtDialog(theDialog, theDialog->visRgn);
  120.     FrameDefault(theDialog, OKItem);
  121.     EndUpdate(theDialog);
  122.     SetPort(savePort);
  123. }
  124.  
  125. /*
  126.  * Frame the OK button with a nice bold outline.  This is from IM I-407.
  127.  */
  128. static void FrameDefault(DialogPtr dlog, int item)
  129. {
  130.     int type;
  131.     Handle hndl;
  132.     Rect box;
  133.     PenState savePen;
  134.  
  135.     GetPenState(&savePen);
  136.     PenNormal();
  137.     GetDItem(dlog, item, &type, &hndl, &box);
  138.     PenSize(3, 3);
  139.     InsetRect(&box, -4, -4);
  140.     FrameRoundRect(&box, 16, 16);
  141.     SetPenState(&savePen);
  142. }
  143.